package com.fann;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.mysql.jdbc.Driver;

public class Member {

	String useridMsg = "";
	String nicknameMsg = "";
	String pwMsg = "";
	String emailMsg = "";
	
	String userid = "";
	String nickname = "";
	String pw1 = "";
	String pw2 = "";
	String email = "";
	
	String loginMsg = "";
	boolean login;
	
//Construction START
	public Member() {
		
	}
	
	public Member(String userid, String nickname, String pw1, String pw2, String email) {
		super();
		this.userid = userid;
		this.nickname = nickname;
		this.pw1 = pw1;
		this.pw2 = pw2;
		this.email = email;
	}

	public Member(String userid, String pw1) {
		super();
		this.userid = userid;
		this.pw1 = pw1;
	}
//Construction END
	
//register START
	public boolean registerValidate() {
		boolean registerValidate = true;
		
		if (userid.length() <4 || userid.length() >20){
			registerValidate = false;
			useridMsg = "userid Length mismatch";
		}
		if (nickname.length() <6 || nickname.length() >20){
			registerValidate = false;
			nicknameMsg = "nickname Length mismatch";
		}
		if (!pw1.equals(pw2)){
			registerValidate = false;
			pwMsg = "Wrong Password";
		}
		if (email.indexOf("@") == -1){
			registerValidate = false;
			emailMsg = "email Format mismatch";
		}
		if (registerValidate){
			
			Connection conn = null;
			PreparedStatement pstmt = null;
			try {
				DriverManager.registerDriver(new Driver());
				conn = DriverManager.getConnection("jdbc:mysql://localhost/thematic","root","");
				
				String sql = "INSERT INTO users(id, nickname, password, email) values (?, ?, ?, ?)";
				pstmt = conn.prepareStatement(sql);
				pstmt.setString(1, userid);
				pstmt.setString(2, nickname);
				pstmt.setString(3, pw1);
				pstmt.setString(4, email);
				int rowCount = pstmt.executeUpdate();
				System.out.println(rowCount);
				
			} catch(SQLException e){
				registerValidate = false;
				e.printStackTrace();
			} finally {
				try {
					if (conn != null && pstmt != null) {
						pstmt.close();
						conn.close();
					}
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
		
		return registerValidate;
	}
//register END
	
//Login START
	public boolean loginValidate() {

        Connection conn = null;
        PreparedStatement pstmt = null;
        login = false;
        
		if (userid.length() <4 || userid.length() >20){
			login = false;
			useridMsg = "userid Length mismatch";
		}
		
		try{
			DriverManager.registerDriver(new com.mysql.jdbc.Driver());
			conn = DriverManager.getConnection("jdbc:mysql://localhost/thematic","root","");
			
			String strSql = "select nickname, email from users where id=? and password=?";
			pstmt = conn.prepareStatement(strSql);
			pstmt.setString(1, userid);
			pstmt.setString(2, pw1);
			ResultSet rs = pstmt.executeQuery();
			if (rs.next()){
				nickname = rs.getString("nickname");
				email = rs.getString("email");
				login = true;
			} else {
				loginMsg = "Login failed";
			}
			
		} catch(SQLException e){
			e.printStackTrace();
		} finally{
			if (conn != null){
				try {
					conn.close();
					pstmt.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
		
		return login;
	}
//Login END
	
//Get&SET
	public String getUseridMsg() {
		return useridMsg;
	}

	public void setUseridMsg(String useridMsg) {
		this.useridMsg = useridMsg;
	}

	public String getNicknameMsg() {
		return nicknameMsg;
	}

	public void setNicknameMsg(String nicknameMsg) {
		this.nicknameMsg = nicknameMsg;
	}

	public String getEmailMsg() {
		return emailMsg;
	}

	public void setEmailMsg(String emailMsg) {
		this.emailMsg = emailMsg;
	}

	public String getPwMsg() {
		return pwMsg;
	}

	public void setPwMsg(String pwMsg) {
		this.pwMsg = pwMsg;
	}

	public String getUserid() {
		return userid;
	}

	public void setUserid(String userid) {
		this.userid = userid;
	}

	public String getNickname() {
		return nickname;
	}

	public void setNickname(String nickname) {
		this.nickname = nickname;
	}

	public String getPw1() {
		return pw1;
	}

	public void setPw1(String pw1) {
		this.pw1 = pw1;
	}

	public String getPw2() {
		return pw2;
	}

	public void setPw2(String pw2) {
		this.pw2 = pw2;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getLoginMsg() {
		return loginMsg;
	}

	public void setLoginMsg(String loginMsg) {
		this.loginMsg = loginMsg;
	}

	public boolean isLogin() {
		return login;
	}

	public void setLogin(boolean login) {
		this.login = login;
	}

}
